package com.sdkx.wear_os.retrofit.response

import com.google.gson.Gson
import com.sdkx.wear_os.retrofit.ApiServiceUrl
import com.sdkx.wear_os.retrofit.Bean
import com.sdkx.wear_os.retrofit.Parameter
import com.sdkx.wear_os.retrofit.UserInfo
import com.sdkx.wear_os.retrofit.api.ApiService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.ResponseBody
import retrofit2.Response
import retrofit2.Retrofit
import java.io.File
import java.net.URLEncoder

/**
 * 请求
 */
class RetrofitManager {
    /**
     * 上传文件
     */
    suspend fun uploadFile(file: File): BaseResponse<Bean> {
        val requestFile: RequestBody =
            file.asRequestBody("multipart/form-data".toMediaTypeOrNull())
        val body =
            MultipartBody.Part.createFormData(
                "file",
                withContext(Dispatchers.IO) {
                    URLEncoder.encode(file.name, "UTF-8")
                },
                requestFile
            )
        return RetrofitUtil.apiService.upLoadFile(
            body
        )
    }

    /**
     * 上传多文件
     */
    suspend fun upLoadsFile(file: List<File>): BaseResponse<List<Bean>> {
        val part = mutableListOf<MultipartBody.Part>()
        file.forEach {
            val requestFile: RequestBody =
                it.asRequestBody("multipart/form-data".toMediaTypeOrNull())
            val body =
                MultipartBody.Part.createFormData(
                    "file",
                    URLEncoder.encode(it.name, "UTF-8"),
                    requestFile
                )
            part.add(body)
        }
        return RetrofitUtil.apiService.upLoadsFile(
            part
        )
    }


    /**
     * 下载文件
     */
    suspend fun downloadFile(url: String): Response<ResponseBody> {
        return Retrofit.Builder().baseUrl(ApiServiceUrl.BASE_URL).build()
            .create(ApiService::class.java)
            .downloadFile(
                url
            )
    }

    /**
     * 账号加密码方式登录
     */
    suspend fun login(parameter: Parameter): BaseResponse<String> {
        return RetrofitUtil.apiService.login(
            Gson().toJson(parameter)
                .toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
        )
    }

    /**
     * 忘记密码
     */
    suspend fun updateLoginPasswordByPhone(
        parameter: Parameter
    ): BaseResponse<String> {
        return RetrofitUtil.apiService.updateLoginPasswordByPhone(
            Gson().toJson(parameter)
                .toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
        )
    }

    /**
     * 退出登录
     */
    suspend fun logout(): BaseResponse<String> {
        return RetrofitUtil.apiService.logout()
    }

    /**
     * 关于
     */
    suspend fun aboutUs(): BaseResponse<UserInfo> {
        return RetrofitUtil.apiService.aboutUs()
    }

    /**
     * 升级
     */
    suspend fun getAndroidRvVersion(): BaseResponse<Bean> {
        return RetrofitUtil.apiService.getAndroidRvVersion()
    }

    /**
     * 个人信息
     */
    suspend fun getLoginUserInfo(): BaseResponse<UserInfo> {
        return RetrofitUtil.apiService.getLoginUserInfo()
    }

    /**
     * 个人信息修改
     */
    suspend fun updateInfo(parameter: Parameter): BaseResponse<String> {
        return RetrofitUtil.apiService.updateInfo(
            Gson().toJson(parameter)
                .toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
        )
    }

    /**
     * 天气
     */
    suspend fun getWeatherByToday(): BaseResponse<Bean> {
        return RetrofitUtil.apiService.getWeatherByToday()
    }

}
